package com.liang.exam.controller;

import com.github.pagehelper.PageInfo;
import com.liang.exam.entity.Result;
import com.liang.exam.entity.Teacher;
import com.liang.exam.service.AdminService;
import com.liang.exam.utils.Md5Util;
import com.liang.exam.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @GetMapping("/deleteTeacher")
    public Result<String> deleteTeacher(@RequestParam("id") Integer id) {
        try {
            System.out.println("删除教师id为：" + id);
            adminService.deleteTeacher(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            System.out.println(e);
            return Result.error("删除失败");
        }
    }

    @GetMapping("/resetPassword")
    public Result<String> resetPassword(@RequestParam("id") Integer id) {
        try {
            adminService.resetPassword(id);
            return Result.success("重置密码成功");
        } catch (Exception e) {
            return Result.error("重置密码失败");
        }
    }

    @GetMapping("/addTeacher")
    public Result<String> addTeacher(String account, String userName, String role) {
        try {
            adminService.addTeacher(account, userName, role);
            return Result.success("添加教师成功");
        } catch (Exception e) {
            return Result.error("添加教师失败");
        }
    }

    @GetMapping("/updateTeacher")
    public Result<String> updateTeacher( Integer id, String userName, String role) {
        try {
            adminService.updateTeacher(id, userName, role);
            return Result.success("更新教师信息成功");
        } catch (Exception e) {
            return Result.error("更新教师信息失败");
        }
    }

    @GetMapping("/allTeacher")
    public Result<PageInfo<Teacher>> allTeacher(Integer pageNum, Integer pageSize) {
        PageInfo<Teacher> pageInfo = adminService.selectAll(pageNum, pageSize);
        return Result.success(pageInfo);
    }

    @PostMapping("/findTeacher")
    public Result<PageInfo<Teacher>> findTeacher(@RequestBody Teacher teacher,@RequestParam Integer pageNum,@RequestParam Integer pageSize) {
        PageInfo<Teacher> pageInfo = adminService.findTeacher(teacher, pageNum, pageSize);
        return Result.success(pageInfo);
    }

    @GetMapping("/updatePassword")
    public Result<String> updatePassword(String oldPwd, String newPwd, String confirmNewPwd) {
        if (!newPwd.equals(confirmNewPwd)) {
            return Result.error("两次输入的密码不一致");
        }
        if (oldPwd.equals(newPwd)) {
            return Result.error("新旧密码不能相同");
        }
        if (newPwd.length() < 6) {
            return Result.error("新密码长度必须大于等于6位");
        }

        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer id = (Integer) claims.get("id");
        Teacher teacher = adminService.findTeacherById(id);
        if (!teacher.getPassword().equals(Md5Util.getMd5Encryption(oldPwd))) {
            return Result.error("原密码错误");
        }
        try {
            adminService.updatePassword(newPwd, id);
            return Result.success("修改密码成功");
        } catch (Exception e) {
            return Result.error("修改密码失败");
        }
    }
}
